const path = require('path');
const webpack = require('webpack');
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
// const CompressionPlugin = require("compression-webpack-plugin"); v5.0.0需要指定版本
const resolve = dir => path.join(__dirname, dir);

module.exports = {
	publicPath: './',
	productionSourceMap: false,
	css: {
		extract: IS_PROD, // 注释css热更新
		sourceMap: false,
		loaderOptions: {
			scss: {
				// ⚠️注意：分号不能少
				prependData: `
				@import "@css/var.scss";
				@import "@css/mixins.scss";
				@import "@css/function.scss";
				$src: "${process.env.VUE_APP_OSS_SRC}";
				`
			},
			postcss: {
				plugins: [
					require('postcss-px2rem') ({
						remUnit: 37.5
					})
				]
			}
		}
	},
	configureWebpack: config => {
		/**
		 * 引入cdn资源
		 */
		config.externals= {
			vue: 'Vue',
			"vue-router": 'VueRouter',
			vuex: 'Vuex',
			axios: 'axios',
			vant: 'vant'
		}
	},
	chainWebpack: config => {
		/**
		 * 引入cdn资源
		 */
		const cdn = {
			css: [
				'//cdn.jsdelivr.net/npm/vant@2.12.9/lib/index.css'
			],
			js: [
				'//health-luban.oss-cn-beijing.aliyuncs.com/online/source/js/1b1655b0-4baf-11ea-815e-29b7b0dc6a01.js', // vue@2.5.17
				'//health-luban.oss-cn-beijing.aliyuncs.com/online/source/js/41f66c10-4baf-11ea-815e-29b7b0dc6a01.js', // vue-router@3.0.1
				'//cdn.jsdelivr.net/npm/axios/dist/axios.min.js', // axios@0.21.1
				'//health-luban.oss-cn-beijing.aliyuncs.com/online/source/js/4e9c0830-4baf-11ea-815e-29b7b0dc6a01.js', // vuex@3.1.1
				'//health-luban.oss-cn-beijing.aliyuncs.com/online/source/js/cd4f2410-4bae-11ea-815e-29b7b0dc6a01.js', // lodash@4.17.11

				'//cdn.jsdelivr.net/npm/vant@2.12.9/lib/vant.min.js',
				'//cdn.jsdelivr.net/npm/moment@2.29.1/min/moment.min.js',
				'//cdn.jsdelivr.net/npm/vconsole@3.4.0/dist/vconsole.min.js',
				'//res2.wx.qq.com/open/js/jweixin-1.6.0.js',
				// '//health-luban.oss-cn-beijing.aliyuncs.com/online/source/js/922ce1b0-814a-11eb-a72b-592b37123625.js', // sensors
				// '//health-luban.oss-cn-beijing.aliyuncs.com/online/source/js/fbd8f4d0-e92a-11eb-83ed-6fdc185632f2.js'//md5加密
			]
		};
		config.plugin("html").tap(args => {
			args[0].cdn = cdn;

			// 修复 Lazy loading routes Error
			args[0].chunksSortMode = 'none';
			return args;
		});
		/**
		 * 添加别名alias
		 */
		config.resolve.alias
		.set('@', resolve('src'))
		.set('@assets', resolve('src/assets'))
		.set('@plugins', resolve('src/plugins'))
		.set('@api', resolve('src/api'))
		.set('@config', resolve('src/config'))
		.set('@components', resolve('src/components'))
		.set('@css', resolve('src/assets/css'))
		/**
		 * 修复HMR（热更新）失效
		 */
		config.resolve.symlinks(true);
		/**
		 * 生产环境开启压缩
		 */
		// 生产环境，开启js\css压缩
        // if(IS_PROD) {
        //     config.plugin('compressionPlugin').use(new CompressionPlugin({
        //         test: /\.(js|css|scss)$/, // 匹配文件名
        //         threshold: 10240, // 对超过10k的数据压缩
        //         minRatio: 0.8,
        //         deleteOriginalAssets: true // 删除源文件
        //     }))
        // }
		/**
		 * 删除moment语言包
		 */
		config.plugin('ignore').use(
			new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn$/)
		);
	},
	devServer: {
		open: true,
        disableHostCheck: true,

	}
}